package com.soilflow.info.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.WechatMenu;
import com.soilflow.common.domain.vo.WechatMenuVo;
import com.soilflow.common.utils.DateUtils;
import com.soilflow.info.mapper.LandInfoMapper;
import com.soilflow.info.mapper.WechatMenuMapper;
import com.soilflow.info.service.IWechatMenuService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 菜单Service业务层处理
 * 
 * @author party
 * @date 2023-09-26
 */
@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, WechatMenu> implements IWechatMenuService {

    @Autowired
    private WechatMenuMapper wechatMenuMapper;

    @Autowired
    private WxMpService wxMpService;

    /**
     * 查询菜单
     * 
     * @param id 菜单主键
     * @return 菜单
     */
    @Override
    public WechatMenu selectWechatMenuById(Long id)
    {
        return wechatMenuMapper.selectWechatMenuById(id);
    }

    /**
     * 查询菜单列表
     * 
     * @param wechatMenu 菜单
     * @return 菜单
     */
    @Override
    public List<WechatMenu> selectWechatMenuList(WechatMenu wechatMenu) {
        return wechatMenuMapper.selectWechatMenuList(wechatMenu);
    }

    /**
     * 新增菜单
     * 
     * @param wechatMenu 菜单
     * @return 结果
     */
    @Override
    public int insertWechatMenu(WechatMenu wechatMenu)
    {
        wechatMenu.setParentId(Long.parseLong("0"));
        wechatMenu.setCreateTime(DateUtils.getNowDate());
        return wechatMenuMapper.insertWechatMenu(wechatMenu);
    }

    /**
     * 修改菜单
     * 
     * @param wechatMenu 菜单
     * @return 结果
     */
    @Override
    public int updateWechatMenu(WechatMenu wechatMenu)
    {
        wechatMenu.setUpdateTime(DateUtils.getNowDate());
        return wechatMenuMapper.updateWechatMenu(wechatMenu);
    }

    /**
     * 批量删除菜单
     * 
     * @param ids 需要删除的菜单主键
     * @return 结果
     */
    @Override
    public int deleteWechatMenuByIds(Long[] ids)
    {
        return wechatMenuMapper.deleteWechatMenuByIds(ids);
    }

    /**
     * 删除菜单信息
     * 
     * @param id 菜单主键
     * @return 结果
     */
    @Override
    public int deleteWechatMenuById(Long id)
    {
        return wechatMenuMapper.deleteWechatMenuById(id);
    }

    @Override
    public List<WechatMenuVo> findMenuInfo() {
        List<WechatMenuVo> menuVoList=new ArrayList<>();
        //查询所有菜单list集合
        List<WechatMenu> list = this.list();
        //查询所有菜单中所有的一级菜单
        List<WechatMenu> onecollectList = list.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        //查询一级菜单中的子菜单
        for (WechatMenu wechatMenu : onecollectList) {
            WechatMenuVo onewechatMenuVo=new WechatMenuVo();
            BeanUtils.copyProperties(wechatMenu,onewechatMenuVo);

            List<WechatMenu> twocollectList = this.selectChildren(onewechatMenuVo.getId(),list);
            List<WechatMenuVo> children=new ArrayList<>();
            for (WechatMenu menu : twocollectList) {
                WechatMenuVo wechatMenuVo=new WechatMenuVo();
                BeanUtils.copyProperties(menu,wechatMenuVo);
                children.add(wechatMenuVo);
            }
            onewechatMenuVo.setChildren(children);
            menuVoList.add(onewechatMenuVo);
        }
        return menuVoList;
    }

    @Override
    public void syncMenu() {
        List<WechatMenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(WechatMenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://oa.atguigu.cn/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for(WechatMenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://oa.atguigu.cn#"+twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private List<WechatMenu> selectChildren(Long id, List<WechatMenu> list) {
        List<WechatMenu> twochildrem=new ArrayList<>();
        for (WechatMenu wechatMenu : list) {
            if (wechatMenu.getParentId()==id) {
                twochildrem.add(wechatMenu);
            }
        }
        return twochildrem;
    }
}
